package com.example.feed.multichannel;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.x.uikit.XThreadManager;
import com.x.uikit.channel.feed.IXFeed;
import com.x.uikit.channel.feed.XFeedView;
import com.x.uikit.link.XParams;
import com.x.uikit.channel.feed.AbstractFeedLoadMoreView;
import com.x.uikit.tabpage.XTabPage;

import java.util.ArrayList;
import java.util.List;

public class FeedTabPage extends XTabPage {
    private SwipeRefreshLayout mRefreshLayout;
    private XFeedView mFeedView;
    private ViewModel mViewModel;

    public FeedTabPage(@NonNull Context context) {
        super(context);
        mRefreshLayout = new SwipeRefreshLayout(getContext());
        mRefreshLayout.setOnRefreshListener(mRefreshListener);
        mFeedView = new XFeedView(getContext());
        StaggeredGridLayoutManager manager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mFeedView.setLayoutManager(manager);
        mRefreshLayout.addView(mFeedView);
        mViewModel = new ViewModel();
        mFeedView.setDataSource(mViewModel.getDataSource());
        mFeedView.setCardFactory(CardFactory.get());
        mFeedView.setEventObserver(new CardEventHandler());
        mChannelLoadMoreView = new ChannelLoadMoreView(getContext());
        mChannelLoadMoreView.setOnLoadMoreListener(new AbstractFeedLoadMoreView.OnLoadMoreListener() {
            @Override
            public void onLoadMore(XParams params) {
                mViewModel.loadMore(params, new Runnable() {
                    @Override
                    public void run() {
                        XParams result = new XParams();
                        result.put("num", "10");
                        mChannelLoadMoreView.finishLoad(true, !mViewModel.hasFinish(), result);
                    }
                });
            }
        });
        mFeedView.setLoadMoreView(mChannelLoadMoreView);
        this.addView(mRefreshLayout, -1, -1);
    }

    private final ChannelLoadMoreView mChannelLoadMoreView;

    private SwipeRefreshLayout.OnRefreshListener mRefreshListener = new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mChannelLoadMoreView.reset();
            mViewModel.refresh(new Runnable() {
                @Override
                public void run() {
                    mFeedView.notifyDataSetChanged();
                    mRefreshLayout.setRefreshing(false);
                }
            });

        }
    };

    public void bindChannelInfo(ChannelInfo info) {
        Log.d("jake", "bindChannelInfo " + info.toString());
        mViewModel.setChannelInfo(info);
        mRefreshLayout.setRefreshing(true);
        mRefreshListener.onRefresh();
        switch (info.title) {
            case "本地":
            case "搞笑":
            case "军事":
                mFeedView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
                break;
        }
    }

    private static class ViewModel {
        DataSource dataSource = new DataSource();
        private boolean mHasFinish;


        public DataSource getDataSource() {
            return dataSource;
        }

        public void refresh(Runnable callback) {
            dataSource.refresh();
            mHasFinish = false;
            XThreadManager.mainPostDelayed(callback, 500);
        }

        public void setChannelInfo(ChannelInfo info) {
            dataSource.setChannelInfo(info);
            dataSource.refresh();
        }

        public void loadMore(XParams params, Runnable callback) {
            if (dataSource.getCount() > 30) {
                mHasFinish = true;
            }

            XThreadManager.mainPostDelayed(new Runnable() {
                @Override
                public void run() {
                    dataSource.loadMore();
                    callback.run();
                }
            }, 500);
        }

        public boolean hasFinish() {
            return mHasFinish;
        }

        private static class DataSource implements IXFeed.DataSource {
            ChannelInfo channelInfo;
            List<BaseCardData> mDataList = new ArrayList<>();
            private int refreshIndex = 1;

            public void setChannelInfo(ChannelInfo info) {
                channelInfo = info;
            }

            public void refresh() {
                Log.d("jake", "DataSource refresh");
                if (channelInfo == null) {
                    return;
                }
                Log.d("jake", "DataSource refresh" + channelInfo.toString());
                mDataList.clear();
                for (int i = 0; i < 10; i++) {
                    BaseCardData cardData = new BaseCardData();
                    cardData.cardType = i % refreshIndex == 0 ? CardFactory.CardType.VIDEO : CardFactory.CardType.IMAGE_TEXT;
                    cardData.title = channelInfo.title + "-第" + refreshIndex + "刷- 数据 " + i + "：“中国最神秘导弹”首次亮相？";
                    cardData.thumbnail = "https://p9.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/2544b6fe92664e6e9f532cfcacb3e1da?from=pc";
                    cardData.url = "https://export.shobserver.com/baijiahao/html/476539.html";
                    mDataList.add(cardData);
                }
                refreshIndex++;
            }

            public void loadMore() {
                int start = mDataList.size();
                for (int i = start; i < start + 10; i++) {
                    BaseCardData cardData = new BaseCardData();
                    cardData.cardType = i % refreshIndex == 0 ? CardFactory.CardType.VIDEO : CardFactory.CardType.IMAGE_TEXT;
                    cardData.title = channelInfo.title + "-第" + refreshIndex + "刷- 数据 " + i + "：“中国最神秘导弹”首次亮相？";
                    cardData.thumbnail = "https://p9.toutiaoimg.com/origin/tos-cn-i-qvj2lq49k0/2544b6fe92664e6e9f532cfcacb3e1da?from=pc";
                    cardData.url = "https://export.shobserver.com/baijiahao/html/476539.html";
                    mDataList.add(cardData);
                }
            }

            @Override
            public int getCount() {
                return mDataList.size();
            }

            @Override
            public IXFeed.CardData getCardData(int position) {
                if (position < 0 || position > mDataList.size() - 1) {
                    return null;
                }
                return mDataList.get(position);
            }
        }
    }

}
